package com.chuangyang.fixboxmaster.ui;

import android.os.Bundle;
import android.widget.TextView;

import com.android.volley.Response;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.B2BPartDetail;
import com.chuangyang.fixboxmaster.bean.B2BPartDetailItem;
import com.chuangyang.fixboxmaster.bean.PinedListType;
import com.chuangyang.fixboxmaster.service.FixBoxApi;
import com.chuangyang.fixboxmaster.service.GsonRequest;
import com.chuangyang.fixboxmaster.ui.adapter.B2BPartDetailListAdapter;
import com.chuangyang.fixboxmaster.ui.widgets.PinnedSectionListView;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by vanshu on 15/11/12 11:47.
 */
public class B2BPartListDetailActivity extends  BaseActivity {
    private B2BPartDetailListAdapter mAdapter;
    private ArrayList<B2BPartDetailItem> mB2BPartDetailList= new ArrayList<>();
    private String orderId;
    private TextView mTotalPrice;
    private double totalPrice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("费用清单");
        orderId=   getIntent().getStringExtra("id");
        setContentView(R.layout.actiivty_part_list);

        PinnedSectionListView mListView = (PinnedSectionListView) findViewById(R.id.listView);
        mAdapter = new B2BPartDetailListAdapter(mB2BPartDetailList);
        mListView.setAdapter(mAdapter);
        mTotalPrice=(TextView)findViewById(R.id.tv_total_price);
        loadData();
    }

    private void loadData() {
        HashMap<String, String> param = new HashMap<>();
        param.put("id",orderId);
        addRequest(new GsonRequest<>(FixBoxApi.GET_PART_LIST_FEE, param, B2BPartDetail.B2BPartDetailInfo.class, responseListener));
    }

    private Response.Listener<B2BPartDetail.B2BPartDetailInfo> responseListener = new Response.Listener<B2BPartDetail.B2BPartDetailInfo>() {
        @Override
        public void onResponse(B2BPartDetail.B2BPartDetailInfo response) {
            if (response != null && response.result != null) {
                generateSkillItems(response.result);
                mAdapter.notifyDataSetChanged();
                mTotalPrice.setText(String.valueOf(totalPrice));
            }
        }
    };

    private void generateSkillItems(B2BPartDetail.B2BPartDetailResult[] skills) {
        for (B2BPartDetail.B2BPartDetailResult item : skills) {
            B2BPartDetailItem sectionItem = new B2BPartDetailItem();
            // section
            sectionItem.type = PinedListType.SECTION;
            sectionItem.skillTitle = item.title;
            mB2BPartDetailList.add(sectionItem);
            // item
            for (B2BPartDetail b2BPartDetail : item.parts) {
                B2BPartDetailItem priceItem = new B2BPartDetailItem();
                priceItem.type = PinedListType.ITEM;
                priceItem.skillTitle = b2BPartDetail.title;
                priceItem.price=String.valueOf(b2BPartDetail.price);
                priceItem.num=b2BPartDetail.num;
                mB2BPartDetailList.add(priceItem);
                totalPrice+=b2BPartDetail.amount;
            }
        }
    }
}
